/*
Created By VietRise on 4/6/2014
*/

#include "Hammer.h"

CCAction* Hammer::_appear = NULL;

Hammer::~Hammer()
{
	CC_SAFE_RELEASE(_appear);
}

Hammer::Hammer()
{
	this->initWithSpriteFrameName("blank.png");
	this->setVisible(false);
}

Hammer* Hammer::create()
{
	Hammer* sprite = new Hammer();
	if (sprite)
	{
		sprite->autorelease();
		return sprite;
	}

	CC_SAFE_DELETE(sprite);
	return NULL;
}

void Hammer::createAction()
{	
	CCAnimation* animation;
	animation = CCAnimation::create();
	CCSpriteFrame* frame;
	
	for (int i = 1; i<=5; i++)
	{
		char szName[100] = { 0 };
		sprintf(szName, "obj_hammer_0%i.png", i);
		frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(szName);
		animation->addSpriteFrame(frame);
	}

	animation->setDelayPerUnit(TIME_ANIMATION_HAMMER / 5);
	animation->setRestoreOriginalFrame(true);
	_appear = CCAnimate::create(animation);
	_appear->retain();	
}

void Hammer::appear(CCPoint tap)
{
	this->setPosition(tap);
	this->setVisible(true);	

	CCFiniteTimeAction* sequence = CCSequence::create(
		(CCAnimate*)Hammer::_appear->copy()->autorelease(),
		CCCallFuncN::create(this, callfuncN_selector(Hammer::animationAppearDone)),
		NULL);
	
	this->runAction(sequence);
}

void Hammer::animationAppearDone(CCNode* pSender)
{
	pSender->setVisible(false);
}